package com.tcs.gamed.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

public class MakeFolderDialog extends DialogBox {

    private static MakeFolderDialogUiBinder makeUiBinder = GWT
        .create(MakeFolderDialogUiBinder.class);
    
    private static EditFolderDialogUiBinder editUiBinder = GWT
            .create(EditFolderDialogUiBinder.class);

    @UiTemplate("MakeFolderDialog.ui.xml")
    interface MakeFolderDialogUiBinder extends
            UiBinder<Widget, MakeFolderDialog> {}
    
    @UiTemplate("EditFolderDialog.ui.xml")
    interface EditFolderDialogUiBinder extends
            UiBinder<Widget, MakeFolderDialog> {}
    
    @UiField TextBox nameBox;
    @UiField TextArea commentArea;
    @UiField Button okButton;
    @UiField Button cancelButton;
    
    private boolean canceled;
    private String folderName;
    private String folderComment;
    private TreeItem parentItem;
    private TreeItem folderItem;
    
    public static MakeFolderDialog createMakeFolderDialog() {
        return new MakeFolderDialog(makeUiBinder);
    }
    
    public static MakeFolderDialog createEditFolderDialog() {
        return new MakeFolderDialog(editUiBinder);
    }

    protected MakeFolderDialog(UiBinder<Widget, MakeFolderDialog> uiBinder) {
        uiBinder.createAndBindUi(this);
        
        nameBox.setText(folderName);
        
        okButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                canceled = false;
                folderName = nameBox.getText();
                folderComment = commentArea.getText();
                hide();
            }
        });
        cancelButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                canceled = true;
                hide();
            }
        });
    }
    
    @UiFactory DialogBox thatsJustMe() {
        return this;
    }

    public String getFolderName() {
        return folderName;
    }

    public void setFolderName(String folderName) {
        this.folderName = folderName;
    }

    public String getFolderComment() {
        return folderComment;
    }

    public void setFolderComment(String folderComment) {
        this.folderComment = folderComment;
    }
    
    public TreeItem getParentFolder() {
        return parentItem;
    }

    public void setParentFolder(TreeItem parentItem) {
        this.parentItem = parentItem;
    }

    public TreeItem getFolder() {
        return folderItem;
    }

    public void setFolder(TreeItem folderItem) {
        this.folderItem = folderItem;
    }

    public boolean isCanceled() {
        return canceled;
    }
    
    @Override
    public void show() {
        canceled = false;
        nameBox.setText(folderName);
        commentArea.setText(folderComment);
        super.show();
    }
}
